<?php

namespace app\admin\model;

use think\Model;
use think\Db;
class OrderModel extends Model
{
	//获取所有预约人员信息
	public function getDataList($isyuyue)
	{
		$userid = session('ADMIN_ID');
		$did = Db::name('user') ->where('id',$userid)->value('d_id');

		if($userid==1){
			$where = [];
		}else{
			$where = [
				'o_did' =>$did
			];
		}
		if($isyuyue ==0){
	        return Db::name("order o")
	       	->where(function($query) {
	    		$query->where('o.o_isguoqi', 0)->where('o.o_ischuli',0);
			})
			->where($where)
	        ->join("sufferer s","o.o_sid=s.s_id","LEFT")
	        ->order('o.o_time asc')
	        ->paginate(10)->each(function($item,$key){
	            if((time()-strtotime($item['o_time']))/60>5){
	            	$data = [
	            		'o_isguoqi'=>1
	            	];
	            	Db::name('order')->where('o_id',$item['o_id'])->update($data);
	            }
	            return $item;
	        });
		}else{
	        return Db::name("order o")
	       	->where(function($query) {
	    		$query->where('o.o_isguoqi', 1)->whereor('o.o_ischuli',1);
			})
			->where($where)
	        ->join("sufferer s","o.o_sid=s.s_id","LEFT")
	        ->order('o.o_time asc')
	        ->paginate(10)->each(function($item,$key){
	            if((time()-strtotime($item['o_time']))/60>5){
	            	$data = [
	            		'o_isguoqi'=>1
	            	];
	            	Db::name('order')->where('o_id',$item['o_id'])->update($data);
	            }
	            return $item;
	        });
		}

	}
	//获取当前预约详情
	public function getOrderInfo($id)
	{
		$data = Db::name('order o')->join("sufferer s","o.o_sid=s.s_id","LEFT")->find($id);
		$data['thumb'] = Db::name('picture')->where('p_oid',$id)->select();
		return $data;
	}
	//获取问诊单详情
	public function getWenzhenInfo($id)
	{
		$wenzhendata = Db::name('wenzhen') ->where('w_oid',$id)->select()->toArray();
		foreach ($wenzhendata as $key => $value) {
			$wenzhendata[$key]['w_qid'] = Db::name('question') ->where('q_id',$value['w_qid'])->value('q_content');
			$wenzhendata[$key]['w_aid'] = Db::name('answer') ->where('a_id','in',$value['w_aid'])->select()->toArray();
		}
		return $wenzhendata;
	}
	//处理预约
	public function dodel($id)
	{
		$data=[
			'o_ischuli' =>1
		];	
		return Db::name("order")->where("o_id",$id)->update($data)?true:false;
	}
	//问诊页面的问题和答案列表
	public function getQuestions()
	{
		$userid = session('ADMIN_ID');
		$did = Db::name('user') ->where('id',$userid)->value('d_id');
		if($did!=0){
			$where = [
				'q_did' =>$did
			];
		}else{
			$where = [];
		}
		$questiondata = Db::name('question') ->where($where)     
			->paginate(10)->each(function($item,$key){
			$item['answerdata'] = Db::name('answer') ->where('a_qid',$item['q_id'])->select();
            return $item;
        });
		return $questiondata;

	}
}